var prefix = jsctx + "/orderDetail";

var orderDetail = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        var userTenantCode = $("#userTenantCode").val();

        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listOrderDetail",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 10,
                    pageList: [10,20,30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo:true,
                    height:h,
                    showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            nickname: $("#nickname").val().trim(),
                            startTime: $("#startTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            mobile: $("#mobile").val().trim(),
                            //orderNum: $("#orderNum").val().trim(),
                            totalMoney: $("#totalMoney").val().trim(),
                            epName: $("#epName").val().trim(),
                            pileNo: $("#pileNo").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'stationName', title: '桩群名', width:'100',align: 'left'},
                        {field: 'pileNo', title: '桩编号', width:'100',align: 'left'},
                        {field: 'brandName', title: '桩厂家', width:'100',align: 'left'},
                        {field: 'orderNum', title: '订单号', width:'100',align: 'left'},
                        {
                            field: 'nickname', title: '会员名', width: '80', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.hlhtTenantCode != null && row.hlhtTenantCode != '') {
                                    if (userTenantCode == row.tenantCode) {
                                        return row.nickname
                                    } else {
                                        return row.hlhtNickname
                                    }
                                }else {
                                    return row.nickname
                                }
                            }
                        },
                        {
                            field: 'mobile', title: '手机号', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.hlhtTenantCode != null && row.hlhtTenantCode != '') {
                                    if (userTenantCode == row.tenantCode) {
                                        return row.mobile
                                    } else {
                                        return row.hlhtMobile
                                    }
                                }else {
                                    return row.mobile
                                }
                            }
                        },
                        {field: 'beginTime', title: '开始充电时间', width:'100',align: 'left'},
                        {field: 'endTime', title: '结束充电时间', width:'100',align: 'left'},
                        {field: 'power', title: '电量<br>(度)', width:'100',align: 'right'},
                        {field: 'elecAmount', title: '基本电费', width:'100',align: 'right'},
                        {field: 'serviceAmount', title: '服务费', width:'100',align: 'right'},
                        {field: 'lastTime', title: '充电时长', width:'100',align: 'right'},
                        {field: 'chargeAmount', title: '总价格<br>(元)', width:'100',align: 'right'},
                        {field: 'elecPrice', title: '电费单价<br>(元/度)', width:'100',align: 'right'},
                        {field: 'servicePrice', title: '服务费单价<br>(元/度)', width:'100',align: 'right'},
                    ]
                });
    }

    function reLoad() {
        var startTime = $("#startTime").val();
        if (startTime == '' || startTime == null){
            parent.layer.alert("请选择开始时间");
            return;
        }
        $('#exampleTable').bootstrapTable('refresh');
    }


    function goBack() {
        location.href = prefix + "/index";
    }

    function exportOrderDetail() {
        $.fileDownload(prefix + "/exportOrderDetail", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                nickname : $("#nickname").val().trim(),
                startTime : $("#startTime").val().trim(),
                totalMoney : $("#totalMoney").val().trim(),
                mobile : $("#mobile").val().trim(),
                endTime : $("#endTime").val().trim(),
                epName : $("#epName").val().trim(),
                //orderNum : $("#orderNum").val().trim(),
                pileNo : $("#pileNo").val().trim(),
                count : $("#count").val() == '' ? 0 : $("#count").val(),
                offset : $('.active .page-link').text()||1,
                limit :$(".page-size").text(),
            },prepareCallback: function (url) {
                layer.msg("文件下载中...",{time:-1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！",{time:1500});
            },
            failCallback: function (data) {
                var str = "{"+data.substring(data.indexOf("{")+1,data.indexOf("}"))+"}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        exportOrderDetail: exportOrderDetail
    };
})();
